<?php
//
// 图片API接口
//
class Controller_API_Image extends Controller
{
    public function __construct ($req, $res)
    {
        parent::__construct($req, $res);
    }
    
    // 用户头像上传接口
    public function action_shaidan ($uid = 0)
    {
        Kohana::$log->add(Log::DEBUG, "image::action_shaidan: uid: $uid");
        $ret = array('status' => 0, 'errormsg' => '');

        if (! empty($_POST))
        {
            try {
                if ($uid <= 0)
                    throw new Exception("image::action_shaidan: 用户未登录");

                $user = User::getUserFromUid($uid);
                
                $strBase64 = Arr::get($_POST, 'photo', '');

                Kohana::$log->add(Log::DEBUG, "image::action_shaidan: base64: $strBase64");
                $binImgContent = base64_decode($strBase64);

                $tmpDir = ini_get('upload_tmp_dir');
                $tmpDir = empty($tmpDir) ? '/tmp' : $tmpDir;
                $filePath = $tmpDir.'/'.uniqid();
                Kohana::$log->add(Log::DEBUG, "image::action_shaidan: $filePath");

                $fh = fopen($filePath, 'wb');
                fwrite($fh, $binImgContent);
                fclose($fh);

                list($url, $path) = Media::save_image_from_file ($filePath, Kohana::config('env.image.avatar'));
//                $user->setUserIcon($url);

                @unlink($filePath);
                Kohana::$log->add(Log::DEBUG, "image::action_shaidan: $url, $path");
                $ret = array('status' => 1, 'errormsg' => $url);

            }
            catch (Exception $e)
            {
                Kohana::$log->add(Log::ERROR, "image::action_avatar: " . $e->getMessage());
                $ret = array('status' => 0, 'errormsg' => '上传头像处理失败');
            }

        }
        else
        {
            $ret = array('status' => 0, 'errormsg' => '');
        }

        echo json_encode($ret);
    }

    // 用户头像上传接口
    public function action_avatar ($uid = 0)
    {
        Kohana::$log->add(Log::DEBUG, "image::action_avatar: uid: $uid");
        $ret = array('status' => 0, 'errormsg' => '');

        if (! empty($_POST))
        {
            try {
                if ($uid <= 0)
                    throw new Exception("image::action_avatar: 用户未登录");

                $user = User::getUserFromUid($uid);
                
                $strBase64 = Arr::get($_POST, 'photo', '');

                Kohana::$log->add(Log::DEBUG, "image::action_avatar: base64: $strBase64");
                $binImgContent = base64_decode($strBase64);

                $tmpDir = ini_get('upload_tmp_dir');
                $tmpDir = empty($tmpDir) ? '/tmp' : $tmpDir;
                $filePath = $tmpDir.'/'.uniqid();
                Kohana::$log->add(Log::DEBUG, "image::action_avatar: $filePath");

                $fh = fopen($filePath, 'wb');
                fwrite($fh, $binImgContent);
                fclose($fh);

                list($url, $path) = Media::save_image_from_file ($filePath, Kohana::config('env.image.avatar'));
                $user->setUserIcon($url);

                @unlink($filePath);
                Kohana::$log->add(Log::DEBUG, "image::action_avatar: $url, $path");

            }
            catch (Exception $e)
            {
                Kohana::$log->add(Log::ERROR, "image::action_avatar: " . $e->getMessage());
                $ret = array('status' => 1, 'errormsg' => '上传头像处理失败');
            }

        }
        else
        {
            $ret = array('status' => 0, 'errormsg' => '');
        }

        echo json_encode($ret);
    }

    // 闪屏页图片接口
    // 若没有闪屏图，则图片地址为空
    // 若有闪屏图，则图片地址为http://开头的一张图片URL
    public function action_splash ()
    {
        try {
            $imgUrl = App_Splash::currentImageUrl();
            $ret  = array(
                'status' => 0,
                'errormsg' => '',
                'imageUrl' => $imgUrl,
            );
        }
        catch (Exception $e)
        {
            $ret = array(
                'status' => 0,
                'errormsg' => $e->getMessage(),
            );
        }

        echo json_encode($ret);
    }
}
